#
# Reading.py
#
"""
::BOH
$Id: reading.py,v 1.3 2007/01/10 00:00:00

Copyright (c) 2006 George Bobeck. All rights reserved.

SOON TO BE REMOVED

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
::EOH

Data structure for OWTF sensor readings
"""
from xml.dom import minidom, Node
class Reading:
    def __init__(self):
        self.mid = "None" # monitor ID
        self.sid = "None" # sensor ID
        self.location = "None" # sensor location 
        self.cron = "0000-01-01 00:00:00" # Datetime Format should be YYYY-MM-DD HH:MM:SS 
        self.temp = 0.0 # temperature, OWFS default
        self.humid = 0.0 # humidity, %
        self.V1 = 0.0 # Voltage 1, extra data slot
        self.V2 = 0.0 # Voltage 2, extra data slot

    def SetMID(self,a):
        self.mid = a
    def GetMID(self):
        return self.mid
    
    def SetSID(self,a):
        self.sid = a
    def GetSID(self):
        return self.sid

    def SetLocation(self,a):
        self.location = a
    def GetLocation(self):
        return self.location

    def SetTime(self,a):
        self.cron = a
    def GetTime(self):
        return self.cron

    def SetCron(self,a):
        """
        ALIAS for SetTime()
        """
        self.cron = a
    def GetCron(self):
        """
        ALIAS for GetTime()
        """
        return self.cron

    def SetTemp(self,a):
        self.temp = float(a)
    def GetTemp(self):
        return self.temp

    def SetHumid(self,a):
        self.humid = float(a)
    def GetHumid(self):
        return self.humid

    def SetV1(self,a):
        self.V1 = float(a)
    def GetV1(self):
        return self.V1

    def SetV2(self,a):
        self.V2 = float(a)
    def GetV2(self):
        return self.V2

    def CtoF(self):
        cel = float( self.temp )
        ft = ( cel * ( 9.0/5.0 ) ) + 32.0
        return ft
    def FtoC(self):
        ft = float( self.temp )
        cel = ((ft - 32.0) * (5.0/9.0))
        return cel
    def CtoK(self):
        kel = self.temp + 273.18
        return kel
    def KtoC(self):
        cel = self.temp - 273.18 
        return cel

    def toXML(self):
        doc = minidom.Document()
        #create <reading>
        reading = doc.createElement('reading')
        doc.appendChild(reading)
        #create <monitor>
        monitor = doc.createElement('monitor')
        reading.appendChild(monitor)
        monitorID = doc.createElement('monitor-name')
        monitorID.appendChild(doc.createTextNode(self.mid))
        monitor.appendChild(monitorID)
        # create <sensor>
        sensor = doc.createElement('sensor')
        monitor.appendChild(sensor)
        sensorID = doc.createElement('sensor-name')
        sensorID.appendChild(doc.createTextNode(self.sid))
        sensor.appendChild(sensorID)
        location = doc.createElement('location')
        location.appendChild(doc.createTextNode(self.location))
        sensor.appendChild(location)
        #create <values>
        values = doc.createElement('values')
        sensor.appendChild(values)
        cron = doc.createElement('cron')
        cron.appendChild(doc.createTextNode(self.cron))
        values.appendChild(cron)
        temp = doc.createElement('temp')
        temp.appendChild(doc.createTextNode(str(self.temp)))
        values.appendChild(temp)
        humid = doc.createElement('humid')
        humid.appendChild(doc.createTextNode(str(self.humid)))
        values.appendChild(humid)
        V1 = doc.createElement('V1')
        V1.appendChild(doc.createTextNode(str(self.V1)))
        values.appendChild(V1)
        V2 = doc.createElement('V2')
        V2.appendChild(doc.createTextNode(str(self.V2)))
        values.appendChild(V2)
        return doc.toxml()

    def toPrettyXML(self,indent = '     '):
        """
        Similar to toXML(), however, this is meant to be
        human readable.  This will add some whitespace  
        (one space)before and after each Text Node.
        """
        doc = minidom.Document()
        #create <reading>
        reading = doc.createElement('reading')
        doc.appendChild(reading)
        #create <monitor>
        monitor = doc.createElement('monitor')
        reading.appendChild(monitor)
        monitorID = doc.createElement('monitor-name')
        monitorID.appendChild(doc.createTextNode(self.mid))
        monitor.appendChild(monitorID)
        # create <sensor>
        sensor = doc.createElement('sensor')
        monitor.appendChild(sensor)
        sensorID = doc.createElement('sensor-name')
        sensorID.appendChild(doc.createTextNode(self.sid))
        sensor.appendChild(sensorID)
        location = doc.createElement('location')
        location.appendChild(doc.createTextNode(self.location))
        sensor.appendChild(location)
        #create <values>
        values = doc.createElement('values')
        sensor.appendChild(values)
        cron = doc.createElement('cron')
        cron.appendChild(doc.createTextNode(self.cron))
        values.appendChild(cron)
        temp = doc.createElement('temp')
        temp.appendChild(doc.createTextNode(str(self.temp)))
        values.appendChild(temp)
        humid = doc.createElement('humid')
        humid.appendChild(doc.createTextNode(str(self.humid)))
        values.appendChild(humid)
        V1 = doc.createElement('V1')
        V1.appendChild(doc.createTextNode(str(self.V1)))
        values.appendChild(V1)
        V2 = doc.createElement('V2')
        V2.appendChild(doc.createTextNode(str(self.V2)))
        values.appendChild(V2)
        return doc.toprettyxml(indent)
